﻿using System;
using CoreComponents.DataAccess;
using Crowler.Model.CoreComponents.DataAccess;
using Crowler.TestBase;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;

namespace Crowler.CoreComponents.Test
{
    [TestClass]
    public class UnitOfWorkFactoryShould:TestBaseClass
    {
        private IUnitOfWorkFactory _unitOfWorkFactory;
        public override void TestCleanup()
        {
            _unitOfWorkFactory.DisposeUnitOfWork(null);
        }

        public override void TestInitialize()
        {
            var configurationProvider = new NHibernateTestConfigurationProvider();
            _unitOfWorkFactory = new UnitOfWorkFactory(configurationProvider);
        }

        [TestMethod]
        public void CreateUnitOfWork()
        {
            IUnitOfWork implementor = _unitOfWorkFactory.Create();
            Assert.IsNotNull(implementor);
            Assert.IsNotNull(_unitOfWorkFactory.CurrentSession);
            Assert.AreEqual(FlushMode.Commit, _unitOfWorkFactory.CurrentSession.FlushMode);
        }

        [TestMethod]
        public void CreateAndAccessSessionFactory()
        {
            var sessionFactory = _unitOfWorkFactory.SessionFactory;
            Assert.IsNotNull(sessionFactory);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ThrowExceptionWhenSessionIsNotOpenOn()
        {
            var session = _unitOfWorkFactory.CurrentSession;
        }
    }
}
